package project.layer.agents;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import project.data.PickUpIntention;
import project.layer.physical.Path;
import project.layer.physical.Truck;
import framework.layer.agent.Agent;
import project.layer.physical.Package;


public class TruckAgent extends Agent implements ReportReceiver{

	private Path intention;
	private TruckAgentState state;
	private ArrayList<Path> desires = new ArrayList<Path>();
	
	
	public TruckAgent(){
		super();
		this.desires = new ArrayList<Path>();
		this.state = new TruckAgentStatePickingUpPackage(this);
	}
	
	
	@Override
	public void processTick(long timePassed) {
		getState().processTruckTick();
	}

	@Override
	public void executeDeploymentOptions() {
		// TODO Auto-generated method stub
		
	}
	
	public Truck getTruck(){
		return (Truck)getDevice().getPhysicalEntity();
	}
	
	public TruckAgentState getState(){
		return this.state;
	}
	
	public void setState(TruckAgentState state){
		this.state = state;
	}
	
	public void reportPath(Path path, ReportReceiver sendingAgent){
		if(getTruck().isOnConnector() && path.getFirstCrossroadOfPath().equals(getTruck().getConnectorPosition().getConnector())){
			desires.add(path);
		}
		
	}
	public boolean isTruckAgent(){
		return true;
	}
	
	public void removeVisitedDestinationFromIntention(int destinationID){
		intention.removeVisitedDestination(destinationID);
	}
	
	public void removeTakenCrossroadFromIntention(){
		intention.removeFirstCrossroadFromPath();
		if(intention.getListOfCrossroadsPath().size() == 0){
			setIntention(null);
		}
	}
	
	
	public Package getPackage(){
		if(getState().isDeliveringPackage()){
			return 	getTruck().getPackage();
		}
		else{
			return null;
		}
			
	}
	
	public boolean hasIntention(){
		return getIntention() != null;
	}

	public void reportPathChange(Path followedPath) {
		// TODO Auto-generated method stub
		
	}
	
	public Path getIntention() {
		return intention;
	}
	
	void setIntention(Path newIntention){
		this.intention = newIntention;
	}

	public ArrayList<Path> getDesires() {
		return desires;
	}
	
	void clearDesires(){
		desires.clear();
	}

	public void reportIntentionRejection(PickUpIntention pickIntent, List<Integer> destinationIDs) {
		
		if(getIntention() == null || matchesCurrentPnDSequence(destinationIDs)){
			if(getTruck().hasPackage()){
				//A pickup intention was rejected during drop off state, finishing drop off
				
				setIntention(getIntention().getPathTillFirstDropOffPoint());
			}
			else{
				setIntention(null);
			}
		}
		
		
		
	}
	
	
	private boolean matchesCurrentPnDSequence(List<Integer> destinationIDs) {
		if(getIntention() == null){
			return false;
		}
		LinkedList<Integer> destinations = getIntention().getDestinations();
		if(destinations.size() != destinationIDs.size()){
			return false;
		}
		for(int i = 0; i < destinations.size(); i++){
			if(destinations.get(i) != destinationIDs.get(i)){
				return false;
			}
		}
		return true;
	}

	public void reportIntention(Path followedPath) {
		
		setIntention(followedPath);
	}

	public boolean hasDesires() {
		return ! desires.isEmpty();
	}

	public void switchToDeliveringState() {
		TruckAgentStateDeliveringPackage newTS = new TruckAgentStateDeliveringPackage(this);
		this.state = newTS;
		
	}
	
	public void switchToPickUpState() {
		TruckAgentStatePickingUpPackage newTS = new TruckAgentStatePickingUpPackage(this);
		this.state = newTS;
		
	}

}
